<?php
header("Content-type: text/plain; charset=utf-8", true);

if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["service-source"])) {
    echo file_get_contents(__FILE__);
    return;
}

session_start();

require_once("lib/jsonss.php");

class DrainService extends JSONSSService {
    public function DisposeMessage($p) {
        if (!isset($_SESSION["ChainedServices.Message"]))
            throw new JSONSSError(502, "Service not ready to dispose message");
        $curmsg = $_SESSION["ChainedServices.Message"] ."";

        unset($_SESSION["ChainedServices.Message"]);
        if ($curmsg != $p)
            throw new JSONSSError(502, "Bad message disposed");
    }

    public function __construct() {
        $this->add_method("DisposeMessage", "disposemsg");
    }
}

$server = new JSONSSServer();
$server->add_service(new DrainService(), "DrainService", "drain-service");
$server->process_request();

session_commit();
